﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Project.MODEL;

namespace Project.DAL.System
{
    public class MenusDal
    {
        /// <summary>
        /// 上下文
        /// </summary>
        PSSEntities entities = DbContextFactory.Create();

        /// <summary>
        /// 获取菜单数据
        /// </summary>
        /// <returns></returns>
        public DbSet<Menus> GetMenus()
        {
            var menus = entities.Menus;
            return menus;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns>添加受影响的行</returns>
        public int AddMenus(Menus menu)
        {
            var datas = entities.Menus;
            datas.Add(menu);
            return entities.SaveChanges();
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns>修改受影响的行</returns>
        public int UpdateMenus(Menus menu)
        {
            var datas = entities.Menus;
            var data = datas.Where(d => d.menuID == menu.menuID).FirstOrDefault();
            if (data == null)
            {
                return -1; //没有找到修改的对象
            }
            else
            {
                //不能修改的项 就不用赋值
                data.menuName = menu.menuName;
                data.areaName = menu.areaName;
                data.actionName = menu.actionName;
                data.controllerName = menu.controllerName;
                data.icon = menu.icon;
                data.menuUrl = menu.menuUrl;

                return entities.SaveChanges();
            }


        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="list"></param>
        /// <returns>删除受影响的行</returns>
        public int DeleteMenus(Menus menu)
        {
            var datas = entities.Menus;
            datas.Remove(menu);
            return entities.SaveChanges();
        }

        /// <summary>
        /// 获取权限信息
        /// </summary>
        /// <returns></returns>
        public DbSet<Authority> GetAuthorities()
        {
            var authority = entities.Authority;
            return authority;
        }


        /// <summary>
        /// 设置权限信息
        /// </summary>
        /// <param name="relist">需要移除的菜单权限</param>
        /// <param name="newlist">新增的菜单权限</param>
        /// <returns></returns>
        public int SetAuthorities(IEnumerable<Authority> relist, IEnumerable<Authority> newlist)
        {
            var authority = entities.Authority;
            authority.RemoveRange(relist);
            authority.AddRange(newlist);
            return entities.SaveChanges();
        }

    }
}
